package com.moyun.common.config.dict.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.moyun.common.annotation.dict.Dict;

import java.io.IOException;

/**
 * 功能描述: 字典序列化 <br/>
 * Dict 字典序列化器
 * 自定义字典序列化逻辑
 */
public class DictSerializer extends StdSerializer<Object> {

    /** 字典注解 */
    private final Dict dict;

    public DictSerializer(Dict dict) {
        super(Object.class);
        this.dict = dict;
    }

    /**
     * 序列化操作
     */
    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeObject(dict.re().getLabel(dict.type(), dict.separator(), value.toString()));
    }

}
